/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.WizardPanel = function(config){
	
	this.type = new GBIF.GeoSpatial.LayerTypes({
		itemId:'combo'
	});

	this.georss = new GBIF.GeoSpatial.Georss({
		itemId:'georss'
	});
	
	this.kml = new GBIF.GeoSpatial.KML({
		itemId:'kml'
	});

	this.WMS = new GBIF.GeoSpatial.WMSCapabilitiesLayers({
		itemId:'wms'
	});
	
	this.WFS = new GBIF.GeoSpatial.WFSCapabilitiesLayers({
		itemId:'wfs'
	});
	
	this.WFS.on('layeraddedtomap',this.addlayerdata,this);

	this.info = new GBIF.GeoSpatial.Information({
		itemId:'info'
	});
	
	this.info.on('docCompleted',this.next,this);
	
	this.wmsfeature = new GBIF.GeoSpatial.FeaturePanel({
		itemId:'wmsfeaturegrid'
	});	
		
	this.data = {};
	
	Ext.apply(this,config,{
			layout:'card'
		,	activeItem: 0
		,	border:true
		,	tbar: [{
					ref: '../moveprev'
				,	text: this.txtBack
				,	scope:this
				,	disabled: true
				,	handler:this.previous
			}, {
					ref: '../movenext'
				,	text: this.txtNext
				,	style:"padding:0px 5px"
				,	scope:this
				,	handler: this.next 
			}]
		,	items: [
					this.type
				,	this.georss
				,	this.kml
				,	this.info
				,	this.WMS
				,	this.WFS
				,	this.wmsfeature
			]
	});
	
	GBIF.GeoSpatial.WizardPanel.superclass.constructor.call(this, config);
}

Ext.extend(GBIF.GeoSpatial.WizardPanel,Ext.Panel, {
		
		next: function(){

			switch(this.layout.activeItem.itemId){
				case 'combo':
					if(this.type.layerType.getValue()==''){
						GBIF.GeoSpatial.Notice.msg(this.txtError,this.txtLayerTypeError);
					} else {
						this.data.mType = this.type.layerType.getValue();
						
						switch(this.data.mType) {
							case 'wms':
								this.clearWMSPanel();
								this.layout.setActiveItem('wms');
								this.WMS.featuredata=[];
								this.WMS.layerdata=[];
								this.WFS.wfslayerdata=[];
								this.movenext.setText(this.txtNext);
								this.WMS.url.focus(); 
								break;

							case 'wfs':
								this.clearWFSPanel();
								this.layout.setActiveItem('wfs');
								this.WFS.wfslayerdata=[];
								this.WMS.featuredata=[];
								this.WMS.layerdata=[];
								this.movenext.setText(this.txtFinish);
								this.WFS.url.focus(); 
								break;

							case 'georss':
								this.layout.setActiveItem('georss');
								this.georss.url.focus(); 
								break;

							case 'kml':
								this.layout.setActiveItem('kml');
								this.kml.url.focus(); 
								break;

							case 'gbif.clb':
								this.layout.setActiveItem('info');
								this.movenext.setText(this.txtFinish);
								this.moveprev.disable();
								this.info.layerId.focus(); 
								break;

							case 'birdlife':
								this.layout.setActiveItem('info');
								this.movenext.setText(this.txtFinish);
								this.moveprev.disable();
								this.info.layerId.focus(); 
								break;
						}
						this.moveprev.enable();
					}
					break;
					
				case 'georss':
					if (this.georss.url.getValue() == '') {
					GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtUrlError);
					} else {
						this.data.url = this.georss.url.getValue();
						this.layout.setActiveItem('info');
						this.movenext.setText(this.txtFinish);
					}
					break;
						
				case 'kml':
					if (this.kml.url.getValue() == '') {
							GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtUrlError);
					} else {
						this.data.url = this.kml.url.getValue();
						this.layout.setActiveItem('info');
						this.movenext.setText(this.txtFinish);
					}
					break;
						
				case 'wms':
					this.WMS.loadLayer();//fireEvent('selectionChanged',this);
					var featurerequest = this.featureRequest();
					if (featurerequest) {
						this.layout.setActiveItem('wmsfeaturegrid');
						this.movenext.setText(this.txtFinish);
					} else {
						this.sendWMSLayerData();
						this.layout.setActiveItem('combo');
						this.movenext.setText(this.txtNext);
						this.moveprev.disable();
					}
					break;	
					
				case 'wmsfeaturegrid':
					this.sendWMSLayerData();
					this.sendFeatureLayerData();
					this.reset();
					this.layout.setActiveItem('combo');
					this.movenext.setText(this.txtNext);
					this.moveprev.disable();
					break;
					
				case 'wfs':
					this.WFS.loadLayer()
					this.sendWFSLayerData();
					this.reset();
					this.layout.setActiveItem('combo');
					this.movenext.setText(this.txtNext);
					this.moveprev.disable();
					break;
							
				case 'info':
					if(this.info.layerId.getValue()=='' || this.info.layertitleId.getValue()==''){
						GBIF.GeoSpatial.Notice.msg(this.txtError,this.txtLayerNameError);
					} else {
						this.sendLayerData();
						this.reset();
						this.layout.setActiveItem('combo');
						this.movenext.setText(this.txtNext);
						this.moveprev.disable();
					}
					break;
			}
		}

	,	sendWFSLayerData:function(){
			var data = this.WFS.wfslayerdata;
			if(Ext.isEmpty(data)){
				return;
			} else {
				Ext.each(data,function(item){
					item.data.mType = 'wfs';
					this.fireEvent("addLayerNode", item.data);
				}, this);
			}
		}
	
	,	sendWMSLayerData:function(){
			var data = this.WMS.layerdata;
			if(Ext.isEmpty(data)){
				return;
			} else {
				Ext.each(data,function(item){
					item.data.mType = 'wms';
					item.data.description = item.data.abstract;
					this.fireEvent("addLayerNode", item.data);	
				}, this);
			}
		}
	
	,	sendLayerData:function(){
			this.data.name=this.info.layerId.getValue();
			this.data.title=this.info.layertitleId.getValue();
			this.data.description =this.info.descId.getValue();
			this.fireEvent("addLayerNode", this.data ); 
		}
			
	,	sendFeatureLayerData:function(){
			var data = this.wmsfeature.featuredata;
			if(Ext.isEmpty(data)){
				return;
			} else {
				Ext.each(data,function(item){
					item.data.mType='wms';
					this.fireEvent("addLayerNode", item.data);	
				}, this);
			}
		}
	
	,	clearWMSPanel:function(){
			this.WMS.url.setValue('');	
			this.WMS.geoGridPanel.store.removeAll();
			this.WMS.clearLayer();
		}
		
	,	clearWFSPanel:function(){
			this.WFS.url.setValue('');	
			this.WFS.geoGridPanel.store.removeAll();
		}	
	
	,	previous: function(){
			
			switch(this.layout.activeItem.itemId){
				case 'georss':
					this.movePrev();
					break;
	
				case 'kml':
					this.movePrev();
					break;
	
				case 'wms':
					this.movePrev();
					break;	
	
				case 'wfs':
					this.movePrev();
					break;
	
				case 'wmsfeaturegrid':
					this.layout.setActiveItem('wms');
					this.movenext.setText(this.txtNext);
					break;
	
				case 'info':
					switch(this.type.layerType.getValue()) {
						case 'georss':
							this.layout.setActiveItem('georss');
							this.georss.url.focus(); 
							break;
	
						case 'kml':
							this.layout.setActiveItem('kml');
							this.kml.url.focus(); 
							break;
	
						case 'gbif.clb':
							this.layout.setActiveItem('combo');
							this.type.layerType.focus(); 
							this.moveprev.disable();
							break;			
					}	
				 this.movenext.setText(this.txtNext);
				 break;
			}
		}
	
	,	reset: function(){
			this.type.layerType.reset();
			this.georss.url.setValue('');
			this.kml.url.setValue('');
			this.info.layerId.setValue('');
			this.info.layertitleId.setValue('');
			this.info.descId.setValue('');
			this.data={};
		}
	
	,	movePrev: function(){
			this.layout.setActiveItem('combo');
			this.movenext.setText(this.txtNext);
			this.type.layerType.focus(); 
			this.moveprev.disable();
		}
	
	,	featureRequest:function(){
			var featuredata = this.WMS.featuredata;
			if(!Ext.isEmpty(featuredata)){
				this.wmsfeature.featuredata=featuredata;
				var root = this.wmsfeature.qurablelayertree.getRootNode();
				//this.wmsfeature.featuregrid.store.removeAll();
				root.removeAll();
				Ext.each(featuredata,function(item){
					var data = item.data;
					var presentnode=true;
					Ext.each(root.childNodes,function(node){
						if(node.text==data.title){
							presentnode=false;		
						}
					});

					if(presentnode){
						var node = root.appendChild(new Ext.tree.TreeNode({
								text: data.title
							,	id: data.title	
							,	leaf: true
							,	expanded: true
							,	description: ''
							,	mType: 'wms'
							,	layer: {	
										layers: data.name
									,	name: data.name
									,	url: data.layer.url
									,	version: ''
									,	transparent: "TRUE"
									,	FORMAT: data.layer.params.FORMAT
									,	querable: true
								}
						}));
					}
					
					var datagrid = new GBIF.GeoSpatial.WMSFeatureGrid({
						itemId: data.title
					});
					GBIF.GeoSpatial.BUS.fireEvent('addTab', datagrid, this);
						
				},this);
				return true;
			} else {
				return false;
			}
		}
});